﻿using System;
using System.Data;
using System.Collections.Generic;
using BYFW.SFL;

namespace LDSP.BRL
{
    public partial class Chemical : BYFW.BOL.BaseEntity
    {
        public Chemical()
        {
            m_tableName = "t_chemical";
            m_hasName = true;
            m_tblCacheManager.IsEnable = false;
        }

        protected string m_alias = string.Empty;
        protected string m_CAS = string.Empty;
        protected string m_UN = string.Empty;
        protected string m_unit = string.Empty;
        protected string m_dangerNumber = string.Empty;
        protected string m_goodsNumber = string.Empty;
        protected string m_dangerType = string.Empty;
        protected string m_limitedScope = string.Empty;
        protected string m_catalog = string.Empty;
        protected string m_catalogOrder = string.Empty;
        protected string m_packageType = string.Empty;
        protected string m_chemicalType = string.Empty;
        protected string m_transportationMethod = string.Empty;
        protected string m_englishName = string.Empty;
        protected string m_secondaryDangerous = string.Empty;
        protected string m_exceptionNumber = string.Empty;
        protected string m_limitationNumber = string.Empty;
        protected string m_typeCatalog = string.Empty;

        /// <summary>
        /// 别名
        /// </summary>
        public string Alias
        {
            get { return m_alias; }
            set { m_alias = value; }
        }
        /// <summary>
        /// CAS号
        /// </summary>
        public string CAS
        {
            get { return m_CAS; }
            set { m_CAS = value; }
        }
        /// <summary>
        /// UN号
        /// </summary>
        public string UN
        {
            get { return m_UN; }
            set { m_UN = value; }
        }
        /// <summary>
        /// 计量单位
        /// </summary>
        public string Unit
        {
            get { return m_unit; }
            set { m_unit = value; }
        }
        /// <summary>
        /// 危险货物编号
        /// </summary>
        public string DangerNumber
        {
            get { return m_dangerNumber; }
            set { m_dangerNumber = value; }
        }
        /// <summary>
        /// 海关商品编号
        /// </summary>
        public string GoodsNumber
        {
            get { return m_goodsNumber; }
            set { m_goodsNumber = value; }
        }
        /// <summary>
        /// 主要燃爆危险性分类
        /// </summary>
        public string DangerType
        {
            get { return m_dangerType; }
            set { m_dangerType = value; }
        }
        /// <summary>
        /// 受限范围
        /// </summary>
        public string LimitedScope
        {
            get { return m_limitedScope; }
            set { m_limitedScope = value; }
        }
        /// <summary>
        /// 化学品目录
        /// </summary>
        public string Catalog
        {
            get { return m_catalog; }
            set { m_catalog = value; }
        }
        /// <summary>
        /// 化学品类型
        /// </summary>
        public string ChemicalType
        {
            get { return m_chemicalType; }
            set { m_chemicalType = value; }
        }
        /// <summary>
        /// 目录序号
        /// </summary>
        public string CatalogOrder
        {
            get { return m_catalogOrder; }
            set { m_catalogOrder = value; }
        }
        /// <summary>
        /// 包装类别
        /// </summary>
        public string PackageType
        {
            get { return m_packageType; }
            set { m_packageType = value; }
        }

        /// <summary>
        /// 运输方式
        /// </summary>
        public string TransportMethod
        {
            get { return m_transportationMethod; }
            set { m_transportationMethod = value; }
        }

        /// <summary>
        /// 英文名称
        /// </summary>
        public string EnglishName
        {
            get { return m_englishName; }
            set { m_englishName = value; }
        }

        /// <summary>
        /// 类别或项目
        /// </summary>
        public string TypeCatalog
        {
            get { return m_typeCatalog; }
            set { m_typeCatalog = value; }
        }

        /// <summary>
        /// 次要危险性
        /// </summary>
        public string SecondaryDangerous
        {
            get { return m_secondaryDangerous; }
            set { m_secondaryDangerous = value; }
        }

        /// <summary>
        /// 例外数量
        /// </summary>
        public string ExceptionNumber
        {
            get { return m_exceptionNumber; }
            set { m_exceptionNumber = value; }
        }

        /// <summary>
        /// 限制数量
        /// </summary>
        public string LimitationNumber
        {
            get { return m_limitationNumber; }
            set { m_limitationNumber = value; }
        }

        /// <summary>
        /// 标示此化学品的KEY
        /// </summary>
        public string KEY
        {
            get
            {
                return m_name + CommonOP.Spliter + m_catalog + CommonOP.Spliter + m_alias;
            }
        }

        protected override void FillContainer()
        {
            base.FillContainer();
            m_container.Add("alias", m_alias);
            m_container.Add("CAS", m_CAS);
            m_container.Add("UN", m_UN);
            m_container.Add("unit", m_unit);
            m_container.Add("dangerNumber", m_dangerNumber);
            m_container.Add("goodsNumber", m_goodsNumber);
            m_container.Add("dangerType", m_dangerType);
            m_container.Add("packageType", m_packageType);
            m_container.Add("limitedScope", m_limitedScope);
            m_container.Add("catalog", m_catalog);
            m_container.Add("catalogOrder", m_catalogOrder);
            m_container.Add("chemicalType", m_chemicalType);
            m_container.Add("transportationMethod", m_transportationMethod);
            m_container.Add("englishName", m_englishName);
            m_container.Add("typeCatalog", m_typeCatalog);
            m_container.Add("secondaryDangerous", m_secondaryDangerous);
            m_container.Add("exceptionNumber", m_exceptionNumber);
            m_container.Add("limitationNumber", m_limitationNumber);
        }

        public override bool GetProperties(System.Data.DataRow record)
        {
            if (!base.GetProperties(record))
            {
                return false;
            }
            m_alias = this.GetStringValue(record, "alias");
            m_CAS = this.GetStringValue(record, "CAS");
            m_UN = this.GetStringValue(record, "UN");
            m_unit = this.GetStringValue(record, "unit");
            m_dangerNumber = this.GetStringValue(record, "dangerNumber");
            m_goodsNumber = this.GetStringValue(record, "goodsNumber");
            m_dangerType = this.GetStringValue(record, "dangerType");
            m_limitedScope = this.GetStringValue(record, "limitedScope");
            m_catalog = this.GetStringValue(record, "catalog");
            m_catalogOrder = this.GetStringValue(record, "catalogOrder");
            m_chemicalType = this.GetStringValue(record, "chemicalType");
            m_packageType = this.GetStringValue(record, "packageType");
            m_transportationMethod = this.GetStringValue(record, "transportationMethod");
            m_englishName = this.GetStringValue(record, "englishName");
            m_typeCatalog = this.GetStringValue(record, "typeCatalog");
            m_secondaryDangerous = this.GetStringValue(record, "secondaryDangerous");
            m_exceptionNumber = this.GetStringValue(record, "exceptionNumber");
            m_limitationNumber = this.GetStringValue(record, "limitationNumber");
            return true;
        }

        /// <summary>
        /// 是否存在重复的记录
        /// </summary>
        /// <param name="name"></param>
        /// <param name="catalog"></param>
        /// <returns></returns>
        public bool Exist(string name, string catalog)
        {
            string cond = string.Format("{0}={1} and {2}={3}",
                DB.GetCol(this.m_nameField), DB.GetVal(name.Trim()),
                DB.GetCol("catalog"), DB.GetVal(catalog.Trim()));
            return this.Exist(null, cond, true);
        }

        /// <summary>
        /// 根据化学品名录ID，删除所有化学品
        /// </summary>
        /// <param name="chemicalCatalogId"></param>
        public void DeleteByChemicalCatalogId(int chemicalCatalogId) {
            string sql = string.Format(@"
delete from t_chemical where catalog = {0}"
    , chemicalCatalogId);

            DB.ExecuteScalar(sql);
        }
    }
}
